home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja_t.z / freyja_t / flib.c < prev    next >
C/C++ Source or Header  |  1992-04-14  |  4KB  |  238 lines

  1. /* LIB.C -- Miscellaneous Library Functions
  2.  
  3.     Written July 1988 by Craig Finseth
  4. */
  5.  
  6. #include "fgenlib.h"
  7.  
  8. /* ------------------------------------------------------------ */
  9.  
  10. /* Return the larger of the two numbers. */
  11.  
  12. int
  13. max(a, b)
  14.     int a;
  15.     int b;
  16.     {
  17.     return(a > b ? a : b);
  18.     }
  19.  
  20.  
  21. /* ------------------------------------------------------------ */
  22.  
  23. /* Return the smaller of the two numbers. */
  24.  
  25. int
  26. min(a, b)
  27.     int a;
  28.     int b;
  29.     {
  30.     return(a < b ? a : b);
  31.     }
  32.  
  33.  
  34. /* ------------------------------------------------------------ */
  35.  
  36. /* Return a pointer to the first occurrance of C in STR. */
  37.  
  38. char *
  39. sindex(str, chr)
  40.     char *str;
  41.     char chr;
  42.     {
  43.     while (*str != NUL && *str != chr) str++;
  44.     return(str);
  45.     }
  46.  
  47.  
  48. /* ------------------------------------------------------------ */
  49.  
  50. /* Convert the value in STR to a decimal number and return it in N. 
  51. The number is in base BASE.  Return TRUE if the conversion was
  52. successful, FALSE if the string was not a valid number.
  53.  
  54. This routine allows for leading whitespace and can handle leading + or
  55. - signs. */
  56.  
  57. FLAG
  58. SToN(str, n, base)
  59.     char *str;
  60.     int *n;
  61.     int base;
  62.     {
  63.     unsigned val;
  64.     int minus;
  65.     char chr;
  66.  
  67.     while (*str == SP || *str == TAB) str++;
  68.     if (*str == '-') {
  69.         minus = -1;
  70.         str++;
  71.         }
  72.     else    {
  73.         minus = 1;
  74.         if (*str == '+') ++str;
  75.         }
  76.     for (val = 0; *str; ++str) {
  77.         chr = xtoupper(*str);
  78.         if (xisalpha(chr)) chr -= 'A' - 10;
  79.         else if (xisdigit(chr)) chr -= '0';
  80.         else return(FALSE);
  81.         if (chr >= base) return(FALSE);
  82.         val = val * base + chr;
  83.         }
  84.     *n = val * minus;
  85.     return(TRUE);
  86.     }
  87.  
  88.  
  89. /* ------------------------------------------------------------ */
  90.  
  91. /* Compare two strings.  Return FALSE on not equal, TRUE on equal.
  92. Comparison is case-independant. */
  93.  
  94. FLAG
  95. strequ(a, b)
  96.     char *a;
  97.     char *b;
  98.     {
  99.     while (*a && *b) if (xtolower(*a++) != xtolower(*b++)) return(FALSE);
  100.     if (*a || *b) return(FALSE);
  101.     return(TRUE);
  102.     }
  103.  
  104.  
  105. /* ------------------------------------------------------------ */
  106.  
  107. /* Compare two strings.  Return FALSE on not equal, TRUE on equal.
  108. Comparison is case-independant.  Compare at most LEN characters. */
  109.  
  110. FLAG
  111. strnequ(a, b, len)
  112.     char *a;
  113.     char *b;
  114.     int len;
  115.     {
  116.     while (*a && *b && len-- > 0) {
  117.         if (xtolower(*a++) != xtolower(*b++)) return(FALSE);
  118.         }
  119.     if (len <= 0) return(TRUE);
  120.     if (*a || *b) return(FALSE);
  121.     return(TRUE);
  122.     }
  123.  
  124.  
  125. /* ------------------------------------------------------------ */
  126.  
  127. /* As ANSI version, but no domain limits. */
  128.  
  129. FLAG
  130. xisalnum(c)
  131.     int c;
  132.     {
  133.     return(xisalpha(c) || xisdigit(c));
  134.     }
  135.  
  136.  
  137. /* ------------------------------------------------------------ */
  138.  
  139. /* As ANSI version, but no domain limits. */
  140.  
  141. FLAG
  142. xisalpha(c)
  143.     int c;
  144.     {
  145.     return(xisupper(c) || xislower(c));
  146.     }
  147.  
  148.  
  149. /* ------------------------------------------------------------ */
  150.  
  151. /* As ANSI version, but no domain limits. */
  152.  
  153. FLAG
  154. xisdigit(c)
  155.     int c;
  156.     {
  157.     return(c >= '0' && c <= '9');
  158.     }
  159.  
  160.  
  161. /* ------------------------------------------------------------ */
  162.  
  163. /* Return TRUE if C is a grayspace character (Space, Tab, CR, LF, FF:
  164. see iswhite). */
  165.  
  166. FLAG
  167. xisgray(c)
  168.     char c;
  169.     {
  170.     return(c == TAB || c == SP || c == CR || c == NL || c == FF);
  171.     }
  172.  
  173.  
  174. /* ------------------------------------------------------------ */
  175.  
  176. /* As ANSI version, but no domain limits. */
  177.  
  178. FLAG
  179. xislower(c)
  180.     int c;
  181.     {
  182.     return(c >= 'a' && c <= 'z');
  183.     }
  184.  
  185.  
  186. /* ------------------------------------------------------------ */
  187.  
  188. /* As ANSI version, but no domain limits. */
  189.  
  190. FLAG
  191. xisupper(c)
  192.     int c;
  193.     {
  194.     return(c >= 'A' && c <= 'Z');
  195.     }
  196.  
  197.  
  198. /* ------------------------------------------------------------ */
  199.  
  200. /* As ANSI version, but handle overlapping strings. */
  201.  
  202. void
  203. xstrcpy(dest, src)
  204.     char *dest;
  205.     char *src;
  206.     {
  207.     do    {
  208.         *dest++ = *src;
  209.         } while (*src++);
  210.     }
  211.  
  212.  
  213. /* ------------------------------------------------------------ */
  214.  
  215. /* As ANSI version, but no domain limits. */
  216.  
  217. int
  218. xtolower(c)
  219.     int c;
  220.     {
  221.     return(xisupper(c) ? c + ('a' - 'A') : c);
  222.     }
  223.  
  224.  
  225. /* ------------------------------------------------------------ */
  226.  
  227. /* As ANSI version, but no domain limits. */
  228.  
  229. int
  230. xtoupper(c)
  231.     int c;
  232.     {
  233.     return(xislower(c) ? c + ('A' - 'a') : c);
  234.     }
  235.  
  236.  
  237. /* end of LIB.C -- Miscellaneous Library Functions */
  238.